﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LeadThing.AbpZeroTemplate.Web.MapService
{
    /// <summary>
    /// proxy 的摘要说明
    /// </summary>
    public class proxy : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            var request = context.Request;
            var urlstr = request.Url.ToString();
            int startIndex = urlstr.IndexOf('?');
            var serverUrl = urlstr.Substring(startIndex+1);

            var result= RequestWebAPI(serverUrl);

            //context.Response.ContentType = "text/xml";
            context.Response.Write(result);
        }

        /// 发送请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="sendData">参数格式 “name=王武&pass=123456”</param>
        /// <returns></returns>
        public static string RequestWebAPI(string url)
        {
            string backMsg = "";
            try
            {
                System.Net.WebRequest httpRquest = System.Net.HttpWebRequest.Create(url);
                httpRquest.Method = "GET";
                //这行代码很关键，不设置ContentType将导致后台参数获取不到值
                httpRquest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                //httpRquest.ContentLength = dataArray.Length;

                System.Net.WebResponse response = httpRquest.GetResponse();
                System.IO.Stream responseStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);
                backMsg = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
                responseStream.Close();
                responseStream.Dispose();
            }
            catch (Exception)
            {
                backMsg="无数据";
            }
            return backMsg;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}